const mysql = require('mysql');
const connection = mysql.createConnection({
    host:'127.0.0.1',
    // port:3306,
    user:'root',
    password:'root',
    database:'albx33'
})
module.exports={
    getAllCategory(callback){
        let sql = 'select * from categories';
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    },
    // 增加新的用户
    addNewCategory(data,callback){
        let sql= `INSERT INTO categories SET \`NAME\`='${data.name}',slug='${data.slug}',classname='${data.classname}'`
        connection.query(sql,(err,result)=>{  //connection.query这个方法是什么意思
            callback(err,result)
        })
    },
    // 通过id获取用户的详细信息
    getCategoryById(id,callback){
        let sql = `SELECT * from categories where id = ${id}`;
        connection.query(sql,(err,result)=>{
            callback(err,result[0]);
        })
    },
    // 通过id删除用户信息
    deleteCategoryById(id,callback){
        let sql = `delete from categories where id=${id}`;
        connection.query(sql,(err,result)=>{
            callback(err,result)
        })
    },
    // 编辑用户通过id

    editCategoryById(id,data,callback){
        let sql = `update categories set ? where id =${id}`
        connection.query(sql,data,(err,result)=>{
            callback(err,result)
        })
    },
    deleteMultiple(ids,callback){
        let data = ids.join();
        let sql = `DELETE FROM categories WHERE id in (${data})`;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    }
}